import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

Dialog {
    id: root
    implicitHeight: 350
    implicitWidth: 600
    padding: 0
    anchors.centerIn: parent
    closePolicy: Popup.CloseOnEscape
    background: Rectangle{
        anchors.fill: parent
        color: "#696969"
    }
    modal: false

    property string text: ""
    property string type: "DateTime"

    onTypeChanged: {
        root.implicitWidth = 600;
        tYear.visible = true;
        tMonth.visible = true;
        tDay.visible = true;
        tHour.visible = true;
        tMinute.visible = true;
        tSecond.visible = true;

        if("Date" === type){
            root.implicitWidth = 420;
            tHour.visible = false;
            tMinute.visible = false;
            tSecond.visible = false;
        }

        if("Time" === type){
            root.implicitWidth = 420;
            tYear.visible = false;
            tMonth.visible = false;
            tDay.visible = false;
        }
    }

    ColumnLayout{
        anchors.fill: parent
        spacing: 0
        anchors.bottomMargin: 20

        Rectangle{
            Layout.fillWidth: true
            Layout.preferredHeight: 30
            color: "#FFFFFF"
            Text {
                anchors.fill: parent
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignLeft
                font.pixelSize: 16
                color: Qt.lighter("#000000")
                leftPadding: 10
                text: root.text
            }
        }

        Rectangle{
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.margins: 30
            radius: 6
            color: "#444444"

            RowLayout{
                anchors.fill: parent
                spacing: 0

                MyTumbler{
                    id: tYear
                    text: "年"
                    min: 1900
                    max: 2100
                    Layout.fillHeight: true
                    Layout.fillWidth: true
                    onCurrentChanged: recalDay()
                }

                MyTumbler{
                    id: tMonth
                    text: "月"
                    min: 1
                    max: 12
                    zero: 2
                    Layout.fillHeight: true
                    Layout.fillWidth: true
                    onCurrentChanged: recalDay()
                }

                MyTumbler{
                    id: tDay
                    text: "日"
                    min: 1
                    max: 30
                    zero: 2
                    Layout.fillHeight: true
                    Layout.fillWidth: true
                }

                MyTumbler{
                    id: tHour
                    text: "时"
                    min: 0
                    max: 23
                    zero: 2
                    Layout.fillHeight: true
                    Layout.fillWidth: true
                }

                MyTumbler{
                    id: tMinute
                    text: "分"
                    min: 0
                    max: 59
                    zero: 2
                    Layout.fillHeight: true
                    Layout.fillWidth: true
                }

                MyTumbler{
                    id: tSecond
                    text: "秒"
                    min: 0
                    max: 59
                    zero: 2
                    Layout.fillHeight: true
                    Layout.fillWidth: true
                }
            }
        }

        RowLayout{
            Layout.preferredHeight: 28
            Layout.fillWidth: true
            spacing: 40

            Item {
                Layout.fillWidth: true
            }

            Button{
                text:qsTr("确定")
                Layout.preferredWidth: 120
                Layout.preferredHeight: 26
                //backgroundDefaultColor: "white"
                //contentItemTextColor: "black"
                onClicked: root.accept()
            }

            Button{
                text:qsTr("取消")
                Layout.preferredWidth: 120
                Layout.preferredHeight: 26
                //backgroundDefaultColor: "white"
                //contentItemTextColor: "black"
                onClicked: root.reject()
            }

            Item {
                Layout.fillWidth: true
            }
        }
    }

    function exec(value){
        var dateTime = new Date(value);
        tYear.current = dateTime.getFullYear();
        tMonth.current = dateTime.getMonth()+1;
        tDay.current = dateTime.getDate();
        tHour.current = dateTime.getHours();
        tMinute.current = dateTime.getMinutes();
        tSecond.current = dateTime.getSeconds();

        open();
    }

    //获取时间
    function getDateTime(){
        return new Date(tYear.current, tMonth.current - 1, tDay.current, tHour.current, tMinute.current, tSecond.current);
    }

    //获取时间字符 yyyy-MM-dd hh:mm:ss
    function getText(format){
        return Qt.formatDateTime(getDateTime(), format);
    }

    //重新计算 日
    function recalDay(){
        var year = tYear.current;
        var month = tMonth.current;

        var solarMonths = [1,3,5,7,8,10,12];
        var dayMax = 31;
        if(-1 === solarMonths.indexOf(month)){
            if(2 === month){
                if(0 === year%4)
                    dayMax = 29;
                else
                    dayMax = 28;
            }else
                dayMax = 30;
        }

        if(dayMax !== tDay.max)
            tDay.max = dayMax;
    }
}
